
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import os, sys


sys.path.append("/Users/admin/Documents/stock/UGFAFAFA/code/")
import akshare as ak

from unit import dateneedupdate
from database import inster,last



def update_etf_daily(code):
    
    
    needupdate = False
    startDate = ""
    lastdata = last("etfdaily" ,fitter=("code='%s'" % code),orderby=["date"],asc=False)
    if lastdata == None:
        needupdate = True
    else:
        startDate = lastdata["date"]
        needupdate = dateneedupdate(startDate,"%Y%m%d")
    if needupdate == True:
        print("正在下载ETF code="+code)
        df = ak.fund_etf_hist_sina(symbol=code)
        df = df.sort_values(by='date')
        df["code"] = code
        df['date'] = df['date'].astype('datetime64[ns]').dt.strftime('%Y%m%d')
        df = df.rename(columns={'volume': 'vol'})
        tdf = df[["date","code","open","close","high","low","vol"]]
        inster("etfdaily",columns=["date","code","open","close","high","low","vol"],df=tdf)
        path =  os.path.join(os.path.expanduser("~"), "stockdate/cvs/etf", code + '.csv')
        tdf.to_csv(path)

if __name__ == '__main__':
    update_etf_daily("sz169103")